package org.ilanchong.droidcontroller;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SearchingActivity extends Activity {
	private ImageView mSearchingImageView;
	private AnimationDrawable mSearchingAnim;
	private LinearLayout mTipsContainer;
	private TextView mTips;
	
	private OnTouchListener mTouchListener = new OnTouchListener(){

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				mSearchingAnim.start();
				UpdateSearchedResult task = new UpdateSearchedResult(SearchingActivity.this);
				SearchCommand arg2 = new SearchCommand(Utils.BROADCAST_IP, Utils.BROADCAST_PORT, SearchCommand.CMD_GET_IP_HOSTNAME);	
				task.execute(arg2);
				return true;
			}
			return false;
		}
		
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pc_searching);
		mSearchingImageView = (ImageView) findViewById(R.id.searching_anim);
		mSearchingImageView.setBackgroundResource(R.anim.searching);
		mSearchingImageView.setOnTouchListener(mTouchListener);
		mSearchingAnim = (AnimationDrawable) mSearchingImageView.getBackground();
		mTipsContainer = (LinearLayout) findViewById(R.id.tips_container);
		mTips = (TextView) findViewById(R.id.tips1);
	}

//	@Override
//	public boolean onTouchEvent(MotionEvent event) {
//		if (event.getAction() == MotionEvent.ACTION_DOWN) {
//			mSearchingAnim.start();
//			UpdateSearchedResult task = new UpdateSearchedResult(this);
//			SearchCommand arg2 = new SearchCommand(Utils.BROADCAST_IP, Utils.BROADCAST_PORT, SearchCommand.CMD_GET_IP_HOSTNAME);	
//			task.execute(arg2);
//			return true;
//		}
//		return super.onTouchEvent(event);
//	}
	
	/**
	 * 更新显示搜索结果
	 * @author 后弦小白
	 *
	 */
	public class UpdateSearchedResult extends android.os.AsyncTask<SearchCommand, Integer, SearchResultInfo> {
		private Context mContext;
		public UpdateSearchedResult(Context context){
			mContext = context;
		}

		@Override
		protected SearchResultInfo doInBackground(SearchCommand... params) {
			// TODO Auto-generated method stub
			SearchCommand arg = params[0];
			String localIp = Utils.getLocalIP(mContext);
			return DroidController.getIPHostname(arg.getBroadCastIP(), localIp, arg.getPort(), arg.getCommand());
		}
		
		@Override
		protected void onPreExecute() {
			mTipsContainer.removeAllViews();
			mTips.setText(R.string.getting_computer);
		}
		
		@Override
		protected void onPostExecute(SearchResultInfo result) {
			if (null != result){
				if (result.getResult()){
					// 显示出可控制电脑
					String resultText = result.getComputer().getIP() + "-" + result.getComputer().getHostName();
					int childcount = mTipsContainer.getChildCount();
					boolean isExist = false;
					for(int i=0;i<childcount;i++){
					   View view =  mTipsContainer.getChildAt(i);
					   if(view instanceof CheckBox){
						   CheckBox checkbox = (CheckBox)view;
						   String str = checkbox.getText().toString();
						   if(resultText.equals(str)){
							   isExist = true;
							   break;
						   }
					   }
					}
					
					if (!isExist){
						CheckBox checkbox = new CheckBox(SearchingActivity.this);
						checkbox.setText(resultText);
						checkbox.setChecked(false);
						checkbox.setOnClickListener(mClickListener);
						mTipsContainer.addView(checkbox, 1);
					} else {
						mTips.setText("正在搜索网络……");
					}
				}
			}
		}
		
		private OnClickListener mClickListener = new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent= new Intent();
				intent.setClass(mContext, RemoteControlActivity.class);
				intent.putExtra("ip_hostname", ((CheckBox)v).getText());
				mContext.startActivity(intent);
				finish();
			}
			
		};
		
		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}
}
